<template>
    <v-plain-dialog ref="edit">
        <el-form label-width="220px" label-position="right">
            <v-form-title title="修改价格"/>
            <el-form-item label="票面金额">
            {{billInfo.faceValue}}
            <span class="unit">元</span>
            </el-form-item>
            <el-form-item label="年化利率" v-if="billInfo.biddingMethod === 0" class="has-unit">
                <el-input class="line-input" v-model="billInfo.annualInterestRate" @input="calc(true)"/>
                <span class="unit">%</span>
            </el-form-item>
            <el-form-item label="每十万扣款" v-if="billInfo.biddingMethod === 0" class="has-unit">
                <el-input class="line-input" v-model="billInfo.billEveryDeduction" @input="calc(false)"/>
                <span class="unit">元</span>
            </el-form-item>
            <el-form-item label="贴息额" v-if="billInfo.biddingMethod === 0">
                {{billInfo.discountAmount}}
                <span class="unit">元</span>
            </el-form-item>
            <el-form-item label="转让额" v-if="billInfo.biddingMethod === 0">
                {{billInfo.transferAmount}}
                <span class="unit">元</span>
            </el-form-item>
            <el-form-item>
                <el-button size="small" type="primary" @click="publish()">询价</el-button>
            </el-form-item>
        </el-form>
    </v-plain-dialog>
</template>

<script lang="ts">
    import { Vue, Component } from 'vue-property-decorator'
    import vPlainDialog from '@/components/plain-dialog'
    import vFormTitle from '@/components/form-title'

    @Component({
        components: {
        vPlainDialog,
        vFormTitle
        }
    })

    export default class Merchants extends Vue {
        billId = ''
        billInfo = {}
        async publish () {
            let res: AjaxResponse = await this.$axios.post('g/gateway/bill/initiateinquiry',{
                billId: this.billInfo.billId,
                annualInterestRate: this.billInfo.annualInterestRate,
                everyPlus: this.billInfo.everyPlus,
                discountAmount: this.billInfo.discountAmount,
                transferAmount: this.billInfo.transferAmount,
                billEveryDeduction: this.billInfo.billEveryDeduction
            })
            this.$utils.tips(res)
            if(res.responseCode === 200){
                this.$refs.edit.closeDialog()
                this.$emit('reloadList')
            }
        }
        
        //查询询价详情
        async getDiscountInfo () {
            let res: AjaxResponse = await this.$axios.get(`b/boms/inquiry/${this.billId}`)
            if(res.responseCode === 200){
                this.billInfo = res.data;
            }
        }

        //根据年化利率计算每十万
        calcEveryPLus () {
            return this.$axios.post('g/gateway/calculator/calculate',{
                interestRate: this.billInfo.annualInterestRate,
                faceValue: this.billInfo.faceValue,
                expiryDate: +new Date(this.billInfo.expiryDate),
                adjustDay: this.billInfo.billType == 0 ? 0 : 3
            })
        }
        //根据每十万计算年化
        calcBillRate () {
            return this.$axios.post('g/gateway/calculator/everyplus',{
                everyPlus: this.billInfo.billEveryDeduction,
                faceValue: this.billInfo.faceValue,
                expiryDate: +new Date(this.billInfo.expiryDate),
                adjustDay: this.billInfo.billType == 0 ? 0 : 3
            })
        }
        async calc (isRate) {
            let res = await (isRate ? this.calcEveryPLus() : this.calcBillRate())
            if(isRate){
                this.billInfo.annualInterestRate = res.data.interestYear//调整后年利率
                this.billInfo.everyPlus = res.data.everyPlus
                this.billInfo.discountAmount = res.data.discountAmount
                this.billInfo.transferAmount = res.data.transferAmount
                this.billInfo.billEveryDeduction = res.data.billEveryDeduction//每十万扣款
            }else{
                this.billInfo.annualInterestRate = res.data.interestYear//调整后年利率
                this.billInfo.billRate = res.data.billRate
                this.billInfo.discountAmount = res.data.discountAmount
                this.billInfo.transferAmount = res.data.transferAmount
                this.billInfo.billEveryDeduction = res.data.billEveryDeduction//每十万扣款
            }
        }

        openDialog (row) {
            this.billId = row.inquiryInformationId
            this.getDiscountInfo()
            this.$refs['edit'].openDialog()
        }

        
    }
</script>
<style lang="less" scoped>
.has-unit {
    position: relative;
    .unit {
      position: absolute;
      top: 0;
      left: 142px;
      color: #333;
    }
  }
</style>
